package com.cos.lession47.web.controller;

import com.cos.lession47.common.Const;
import com.cos.lession47.entity.Emp;
import com.cos.lession47.services.EmpServices;
import com.cos.lession47.services.impl.EmpServicesImpl;
import com.cos.lession47.util.SendMessage;
import com.cos.lession47.util.StringUtil;
import lombok.extern.java.Log;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @author: COS
 * @time: 2022/2/28 11:21
 * @description:
 */
@WebServlet("/emp")
//@Log4j//自己帮我们创建Logger对象 默认属性名为log
public class EmpController extends BaseServlet {

    private EmpServices es = new EmpServicesImpl();
    //创建日志对象
    Logger log = LoggerFactory.getLogger(EmpController.class);

    public String checkMsg(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
        log.debug("user request:" + request.getRequestURI() + "--ip:" + request.getLocalAddr());
        //获取用户提交的验证码
        String checkMsg = request.getParameter("msg");
        System.out.println(checkMsg);
        //非空判断
        if (StringUtil.isNotEmpty(checkMsg)) {
            //获取保存早session里面的手机验证码
            String phoneMsg = (String) request.getSession().getAttribute("phoneMsg");
            //判断验证码是否正确
            if (phoneMsg.equals(checkMsg)) {
                return "1";
            }
        }
        return "0";
    }


    public String changePwd(HttpServletRequest request, HttpServletResponse response) {
        String pwd = request.getParameter("pwd");
        Integer eno = Integer.valueOf(request.getParameter("eno"));

        boolean ans = es.changePwd(pwd, eno);
        //跳转登录页面
        return Const.REDIRECT + "login.jsp";
    }

    public String getEmpInfoByPhone(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
        log.debug("user request:" + request.getRequestURI() + "--ip:" + request.getLocalAddr());
        String phone = request.getParameter("phone");
        //访问业务层查询用户数据
        Emp e = es.getEmpInfoByPhone(phone);
        if (e != null) {
            log.debug("user request success by data e:" + e);
            //页面转发
            request.setAttribute("e", e);
            return Const.FORWARD + "updatePassword.jsp";
        }
        log.debug("user request fail");
        request.setAttribute("msg", "手机号有误");
        return Const.FORWARD + "forget.jsp";
    }


    public String sendMessage(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
        log.debug("user request:" + request.getRequestURI() + "--ip:" + request.getLocalAddr());
        String phone = request.getParameter("phone");
        //调用发送短信的工具类
        String s = SendMessage.sendMessage(phone);
        request.getSession().setAttribute("phoneMsg", s);
        return "发送成功,请注意短信";
    }

    public String reg(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Emp e = new Emp();
        BeanUtils.populate(e, parameterMap);
        System.out.println(e);
        boolean ans = es.addEmp(e);
        if (ans) {
            return Const.REDIRECT + "index.jsp";
        }
        request.setAttribute("msg", "注册失败,请重新再试");
        return Const.FORWARD + "reg.jsp";
    }

    public String getAll(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("测试");
        return Const.FORWARD + "index.jsp";
    }

}
